/********************************** (C) COPYRIGHT *******************************
 * File Name          : main.c
 * Author             : WCH
 * Version            : V1.0.0
 * Date               : 2021/06/06
 * Description        : Main program body.
 *********************************************************************************
 * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
 * Attention: This software (modified or not) and binary are used for
 * microcontroller manufactured by Nanjing Qinheng Microelectronics.
 *******************************************************************************/

/*
 *@Note
 USART Print debugging routine:
 USART1_Tx(PA9).
 This example demonstrates using USART1(PA9) as a print debug port output.

*/

#include "debug.h"
#include <string.h>

#include "bsp_gpio.h"
#include "drv_spinor.h"

/* Global typedef */

/* Global define */

/* Global Variable */

/**
 * @brief 
 * 
 */
void Led_GPIO_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure = { 0 };

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    GPIO_SetBits(GPIOC, GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9);
}

uint8_t buf[4096] = { 0 };

/*********************************************************************
 * @fn      main
 *
 * @brief   Main program.
 *
 * @return  none
 */
int main(void)
{
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    SystemCoreClockUpdate();
    Delay_Init();
    USART_Printf_Init(115200);
    Led_GPIO_Init();

    printf("SystemClk:%d\r\n", SystemCoreClock);
    printf("ChipID:%08x\r\n", DBGMCU_GetCHIPID());
    printf("This is printf example\r\n");

    RCC_ClocksTypeDef RCC_Clocks;
    RCC_GetClocksFreq(&RCC_Clocks);
    printf("PCLK1 = %d\r\n", RCC_Clocks.PCLK1_Frequency);
    printf("PCLK2 = %d\r\n", RCC_Clocks.PCLK2_Frequency);

    BSP_SPINor_GPIO_Init();
    SPINor_Init();
    uint16_t NorId = SPINor_ReadID();
    printf("SPI Nor ID = %X\r\n", NorId);

    printf("SR = %X\r\n", SPINor_ReadSR());

    uint8_t id_list[8] = { 0 };
    uint8_t id_list_2[8] = { 0 };
    SPINor_ReadUniqueID(id_list);
    SPINor_ReadUniqueID(id_list_2);

    for (uint8_t i = 0; i < 8; i++) {
        printf("ID[0][%d] = %X\r\n", i, id_list[i]);
        printf("ID[1][%d] = %X\r\n", i, id_list_2[i]);
    }
    if (memcmp((void *)id_list, (void *)id_list_2, 8) == 0) {
        printf("Uniqure ID No Error\r\n");
    }

    uint8_t jedec_id[3] = { 0 };
    SPINor_ReadJedecID(jedec_id);
    for (uint8_t i = 0; i < 3; i++) {
        printf("JEDEC ID[%d] = %X\r\n", i, jedec_id[i]);
    }

    // SPINor_Erase_Chip();
    // printf("chip erase ok\r\n");

    // SPINor_Erase_Sector(0x0);
    // printf("Sector erase ok\r\n");

    char *write_buf = "hello world";
    SPINor_Write_Page((uint8_t *)write_buf, 0x0, 8);

    SPINor_Read(buf, 0x0, 8);
    for (uint16_t i = 0; i < 8; i++) {
        printf("%c\t", buf[i]);
        if ((i + 1) % 8 == 0) {
            printf("\r\n");
        }
    }
    printf("\r\n");

    while (1) {
    }
}
